<template>
  <label class="toggle-switch">
    <slot name="label" v-if="$slots.label">
      {{ label }}
    </slot>
    <input
      type="checkbox"
      class="toggle-switch__input"
      :id="id"
      v-model="localModelValue"
      @change="$emit('update:modelValue', $event.target.checked)"
    />
    <span class="toggle-switch__slider"></span>
  </label>
</template>

<script>
export default {
  name: 'ToggleSwitch',
  props: {
    modelValue: {
      type: Boolean,
      default: false
    },
    label: {
      type: String,
      default: ''
    },
    id: {
      type: String,
      default: () => `toggle-${Math.random().toString(36).substr(2, 9)}`
    }
  },
  emits: ['update:modelValue'],
  computed: {
    localModelValue: {
      get() {
        return this.modelValue;
      },
      set(value) {
        this.$emit('update:modelValue', value);
      }
    }
  }
};
</script>

<style scoped>
.toggle-switch {
  display: inline-flex;
  align-items: center;
  gap: 0.75rem;
  cursor: pointer;
  user-select: none;
  font-size: 1rem;
  color: #333;
}

.toggle-switch__input {
  position: absolute;
  opacity: 0;
  width: 0;
  height: 0;
}

.toggle-switch__slider {
  position: relative;
  flex-shrink: 0;
  width: 3.2rem;
  height: 1.8rem;
  background-color: #e0e0e0;
  border-radius: 1.8rem;
  transition: background-color 0.3s ease;
  box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.1);
}

.toggle-switch__input:checked + .toggle-switch__slider {
  background-color: #4CAF50;
}

.toggle-switch__slider::before {
  content: '';
  position: absolute;
  top: 0.25rem;
  left: 0.25rem;
  width: 1.3rem;
  height: 1.3rem;
  background-color: white;
  border-radius: 50%;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

.toggle-switch__input:checked + .toggle-switch__slider::before {
  transform: translateX(1.7rem);
}

.toggle-switch:hover .toggle-switch__slider {
  box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.15);
}

.toggle-switch:active .toggle-switch__slider::before {
  transform: scale(0.95) translateX(1.7rem);
}

:deep(.toggle-switch__label) {
  font-weight: 500;
}
</style>